package com.chaofan.service;

import com.chaofan.domain.User;
import com.chaofan.domain.UserInfo;
import com.chaofan.domain.constant.UserConstant;
import com.chaofan.domain.exception.ConditionException;
import com.chaofan.mapper.UserMapper;
import com.chaofan.util.MD5Util;
import com.chaofan.util.RSAUtil;
import com.chaofan.util.TokenUtil;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.Objects;

/**
 * @author 李超凡
 * @since 2022/1/14 17:28
 */
@Service
public class UserService {
    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public void addUser(User user) {
        var dbUser = userMapper.selectByPhone(user.getPhone());
        if (Objects.isNull(dbUser)) {
            throw new ConditionException("该手机号已经被使用");
        }
        Date now = new Date();
        var salt = String.valueOf(now.getTime());
        var password = user.getPassword();
        //原密码
        String rawPassword;
        try {
            rawPassword = RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("密码验证失败");
        }
        String sign = MD5Util.sign(rawPassword, salt, "UTF-8");
        user.setSalt(salt);
        user.setPassword(sign);
        user.setCreateTime(now);
        int result = userMapper.insert(user);
        if (result == 0) {
            throw new ConditionException("注册失败");
        }
        //添加用户信息
        var userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setNick(UserConstant.DEFAULT_NICK);
        userInfo.setBirth(UserConstant.DEFAULT_BIRTH);
        userInfo.setGender(UserConstant.GENDER_UNKNOW);
        userInfo.setCreateTime(now);
        userInfo.setSign(sign);
        userMapper.addUserInfo(userInfo);
    }

    public User getUserByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    public String login(User user) throws Exception {
        String phone = user.getPhone();
        if (StringUtils.isNullOrEmpty(phone)) {
            throw new ConditionException("手机号不能为空");
        }
        User dbUser = userMapper.selectByPhone(phone);
        if (dbUser == null) {
            throw new ConditionException("当前用户不存在");
        }
        String password = dbUser.getPassword();
        String rawPassword;
        try {
            rawPassword = RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("密码解密失败");
        }
        String salt = dbUser.getSalt();
        String md5Password = MD5Util.sign(rawPassword, salt, "UTF-8");
        if (!md5Password.equals(dbUser.getPassword())) {
            throw new ConditionException("密码错误");
        }
        return TokenUtil.generateToken(dbUser.getId());

    }

    public User getUserInfo(Long userId) {
        User user = userMapper.selectById(userId);
        UserInfo userInfo = userMapper.selectUserInfoByUserId(userId);
        user.setUserInfo(userInfo);
        return user;
    }

    public void updateUser(User user) {
        userMapper.updateById(user);
    }
}
